package org.wedding.center.pay.utils;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.ijpay.core.kit.PayKit;
import com.ijpay.core.kit.WxPayKit;
import com.ijpay.wxpay.WxPayApi;
import com.ijpay.wxpay.enums.WxApiType;
import com.wechat.pay.java.core.certificate.CertificateDownloader;
import com.wechat.pay.java.core.certificate.CertificateHandler;
import com.wechat.pay.java.core.cipher.AbstractAeadCipher;
import com.wechat.pay.java.core.cipher.AeadCipher;
import groovyjarjarpicocli.CommandLine;
import org.springframework.web.bind.annotation.RequestMethod;

import java.io.*;
import java.security.SecureRandom;
import java.security.cert.*;
import java.util.Map;

public class KeyUtil {
    public static void main(String[] args) {
//        String s = generateAlphanumericKey(32);
//        System.out.println(s);
        getSerialNumber();
//        testCertDownload();
    }

    public static String generateAlphanumericKey(int length) {
        String CHAR_POOL = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz23456789";
        SecureRandom random = new SecureRandom();

        StringBuilder sb = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            int index = random.nextInt(CHAR_POOL.length());
            sb.append(CHAR_POOL.charAt(index));
        }
        return sb.toString();
    }


    //用于证书解密的密钥
    private static String apiV3key = "49e1ybwtChhPQJ5quZdIlSVlSL22yysu";
    // 商户号
    private static String mchId = "1722106733";
    // 商户证书序列号
    private static String mchSerialNo = "22CE0875F5F3BF90D474F29EF1FC91FD6ACBE8FE";
    // 商户私钥
    private static String mchPrivateKeyFilePath = "E:\\workSpace\\bykj\\婚庆小程序\\项目\\frsimple-boot-develop\\service\\wedding-start\\src\\main\\resources\\apiclient_key.key";
    private static String certPath = "E:\\workSpace\\bykj\\婚庆小程序\\项目\\frsimple-boot-develop\\service\\wedding-start\\src\\main\\resources\\apiclient_cert.pem";
    // 微信支付平台证书
    //private static String wechatpayCertificateFilePath = "wxpayplatformcert.pem";
    //下载成功后保存证书的路径
    private static String outputFilePath = "E:\\workSpace\\bykj\\婚庆小程序\\需求\\cert";

    public static void testCertDownload() {
//        String[] args = {"-k", apiV3key, "-m", mchId, "-f", mchPrivateKeyFilePath,
//                "-s", mchSerialNo, "-o", outputFilePath, "-c", wechatpayCertificateFilePath};

        String[] args = {"-k", apiV3key, "-m", mchId, "-f", mchPrivateKeyFilePath,
                "-s", mchSerialNo, "-o", outputFilePath};
        CertificateDownloader.Builder builder = new CertificateDownloader.Builder().downloadUrl(outputFilePath);
        CertificateDownloader certificateDownloader = new CertificateDownloader(CommandLine.populateCommand(builder, args));
        certificateDownloader.download();
    }

    private static String getSerialNumber() {
        X509Certificate certificate = PayKit.getCertificate(FileUtil.getInputStream(certPath));
        String serialNo  = certificate.getSerialNumber().toString(16).toUpperCase();
//        String serialNo = null;
//        // 读取商户私钥
//        // 获取证书序列号
//        X509Certificate certificate = PayKit.getCertificate(certPath);
//        if (null != certificate) {
//            serialNo = certificate.getSerialNumber().toString(16).toUpperCase();
//            // 提前两天检查证书是否有效
//            boolean isValid = PayKit.checkCertificateIsValid(certificate, mchId, -2);
//            System.out.println("证书是否可用 {"+isValid+"} 证书有效期为 {"+DateUtil.format(certificate.getNotAfter(), DatePattern.NORM_DATETIME_PATTERN)+"}");
//        }
//            System.out.println("输出证书信息:\n" + certificate.toString());
//            // 输出关键信息，截取部分并进行标记
//            System.out.println("证书序列号:" + certificate.getSerialNumber().toString(16));
//            System.out.println("版本号:" + certificate.getVersion());
//            System.out.println("签发者：" + certificate.getIssuerDN());
//            System.out.println("有效起始日期：" + certificate.getNotBefore());
//            System.out.println("有效终止日期：" + certificate.getNotAfter());
//            System.out.println("主体名：" + certificate.getSubjectDN());
//            System.out.println("签名算法：" + certificate.getSigAlgName());
//            System.out.println("签名：" + certificate.getSignature().toString());
        System.out.println("serialNo:" + serialNo);
        return serialNo;
    }

}
